根據之前排定的時程,昨天好像有點小超進度(原本要把HelloWorld放在今天來寫的),那麼今天就根據昨天的 HelloWorld 來做延伸吧!
參考的網站:點擊以打開
根據昨天學到的Console.WriteLine,將後面的文字印出來,寫出第一行 What is your name? 作為問題。
Console.ReadLine()是從主控台讀取使用者輸入的方法,並設一個變數name來存使用者所輸入的內容。
再加上昨天學過的DateTime.Now來存取當前時間。
Console.WriteLine("What is your name?");
var name = Console.ReadLine();
var currentDate = DateTime.Now;
Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");
Console.Write($"{Environment.NewLine}Press Enter to exit...");
Console.Read();
執行後就能得到以下結果
變成讓使用者輸入兩個數字,但因為輸入的為文字形態,所以要將input1以及input2轉成int才能做計算,
Console.WriteLine("Enter 2 number:");
var input1 = Console.ReadLine();
var input2 = Console.ReadLine();
int num1 = int.Parse(input1);
int num2 = int.Parse(input2);
Console.Write($"{Environment.NewLine}Two number total is {num1+num2}");
Console.Write($"{Environment.NewLine}Press Enter to exit...");
Console.Read();
執行後會得到以下結果: